<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
//公众号操作员设置。管理公众号操作员，对操作员进行增删查改。
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>array('1','2','4'));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total=0;
	$fields=array('id','uniacid','name','type','status');
	$list=pdo_getslice('core_users',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'操作员列表'),
		),
		'list'=>array('data'=>'list','html_to'=>'list_html','list'=>array(
			array('name'=>'id','title'=>'操作员ID','type'=>'string'),
			array('name'=>'name','title'=>'操作员名称','type'=>'string'),
			array('name'=>'status','title'=>'禁止','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要管理的操作员！','error');
	}
	$post=get_postdata();
	$status=intval($post['status']);
	pdo_update('core_users',array('status'=>$status),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($status){
		exi('禁止成功！该操作员将无法登陆后台！');
	}else{
		exi('恢复成功！该操作员可以正常登陆并执行指定操作了！');
	}
	
}
if($_RQ['op']=='edit'){
	$uid=intval($_RQ['id']);
	if($uid){
		$item=pdo_get('core_users',array('id'=>$uid,'uniacid'=>$_SESSION['uniacid']),array('name','type','id','status','home'));
		$actions=pdo_get('core_user_permissions',array('uid'=>$uid),array('sys_actions','plugin_actions'));
		$item=array_merge($item,$actions);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['types']=array('1'=>'操作员','2'=>'副管理员');
	$return['statuss']=array('0'=>'正常','1'=>'禁用');
	if($_SESSION['isfounder'] && $_SESSION['uniacid']==1){
		$return['types']['4']='副站长';
		$return['sys_actions']=pdo_getall('core_permissions',array('type between'=>array(1,4)),array('id','title'),'id');
		$return['plugin_actions']=pdo_getall('core_plugins',array(),array('id','title'),'id');
	}elseif($_SESSION['isfounder']){//超级管理员切换普通管理员
		$account=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']));
		$useractions=model('core/user')->get_useractions($account);
		$sys_actions=$useractions['sys_actions'];
		$plugin_actions=$useractions['plugin_actions'];
		$sys_actions=explode(';',$sys_actions);
		$return['sys_actions']=pdo_getall('core_permissions',array('id'=>$sys_actions),array('id','title'),'id');
		$plugin_actions=explode(';',$plugin_actions);
		$return['plugin_actions']=pdo_getall('core_plugins',array('id'=>$plugin_actions),array('id','title'),'id');
	}else{
		$sys_actions=trim($_SESSION['user']['sys_actions'],';');
		$sys_actions=explode(';',$sys_actions);
		$return['sys_actions']=pdo_getall('core_permissions',array('id'=>$sys_actions),array('id','title'),'id');
		$plugin_actions=trim($_SESSION['user']['plugin_actions'],';');
		$plugin_actions=explode(';',$plugin_actions);
		$return['plugin_actions']=pdo_getall('core_plugins',array('id'=>$plugin_actions),array('id','title'),'id');
	}
	foreach($return['sys_actions'] as &$action){
		$action=$action['title'];
	}
	if($return['plugin_actions']){
		foreach($return['plugin_actions'] as &$action){
			$action=$action['title'];
		}
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'操作员',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'操作员列表'),
			'1'=>array('op'=>'edit','title'=>'添加操作员'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'操作员名称','name'=>'name', 'help'=>'' ),
			array('type'=>'password','title'=>'操作员密码','name'=>'passwd', 'help'=>'需要设置或修改操作员密码请填此项' ),
			array('type'=>'radio','title'=>'操作员类型','name'=>'type','options'=>'types' ),
			array('type'=>'link','from'=>'manage','title'=>'操作员首页','name'=>'home', 'help'=>'操作员登录时跳转的首页！勿必选择有访问权限的，否则登录后，就会提示无权访问！' ),
			array('type'=>'radio','title'=>'操作员状态','name'=>'status','options'=>'statuss'),
			array('type'=>'checkbox','choose_all'=>true,'title'=>'系统权限设置','name'=>'sys_actions','options'=>'sys_actions','help'=>'设置系统功能权限' ),
			array('type'=>'checkbox','choose_all'=>true,'title'=>'插件权限设置','name'=>'plugin_actions','options'=>'plugin_actions','help'=>'设置插件功能权限' ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['type']=intval($postdata['type']);
	if($postdata['type']==4 && !$_SESSION['isfounder']){
		exi('没有权限！','error');
	}
	if(!in_array($postdata['type'],array('1','2','4'))){
		exi('参数错误！','error');
	}
	$id=intval($postdata['id']);
	$data=array(
		'name'=>$postdata['name'],
		'status'=>intval($postdata['status']),
		'type'=>intval($postdata['type']),
		'home'=>$postdata['home'],
		'password'=>$postdata['passwd'],
		'sys_actions'=>trim($postdata['sys_actions']),
		'plugin_actions'=>trim($postdata['plugin_actions']),
	);

	if($id){
		model('core/operator')->update_operator($id,$data);
		$message='修改操作员成功！';
	}else{
		model('core/operator')->create_operator($data);
		$message='添加操作员成功！';
	}
	exi($message,'','list');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		model('core/operator')->del_operator($id);
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				model('core/operator')->del_operator($id);
			}
		}
	}
	exi('删除成功！');
}
?>